﻿package part_4_HomeWork;

public class Thread10 implements Runnable{

	private int count=10;//记录剩余票数
	private int num=0;//记录当前抢到第几张票
	private boolean flag=false;//记录票是否卖完
	@Override
	public void run() {
		// 循环当剩余票数为0时结束
		while(true){
			synchronized(this){
				if(count<=0){
					flag=true;
					break;
				}
				//1.修改数据（剩余票数，抢到第几张票）
				count--;
				num++;
				//延迟模拟
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//2.显示信息，反馈用户抢到第几张票
				System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票,剩余"+count+"张票");
			}
		}	
	}
}
